package cn.com.xy.sms.sdk.service.baseparse;

import java.io.File;

import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.util.DataEnCipher;
import cn.com.xy.sms.sdk.util.StringUtils;
/* SDK-493 hewngao 20160618 start */
public class DataEnCipherApi {	
	
		public static byte[] mLocalKey;
		public static String mSdkChannel;
		public static String mSdkSecritKey;
		public static boolean mIsLoad =false;
		private static DataEnCipher mDataEnCipher =null;
	    public static String ERROR_CODE="ERROR";
		/**
		 * 解密数据
		 * @param rawData 待解密的数据
		 * @return
		 */
		public static String dataDecrypt(String rawData){
			try{
				if(!init()){
					return ERROR_CODE;
				}
				if(StringUtils.isNull(rawData)){
					return "";
				}
//				StringUtils.decodToBytes(rawData); 
				//先解码
				byte[] dataBuf = mDataEnCipher.xyBase64Decode2(rawData);
				//再解密
				dataBuf = mDataEnCipher.xyDecrypt(dataBuf, dataBuf.length, mLocalKey, mLocalKey.length);
				if(dataBuf == null){
					return "";
				}
				return new String(dataBuf,"UTF-8");
			}catch(Throwable e){
				LogManager.e("xiaoyuan", "DataEnCipher xyDecode: ", e);
			}
			return "";
		}	
		/***
		 * 加密数据
		 * @param data 待加密的字符串
		 * @return
		 */
		public static String dataEncrypt(String data){
			try{
				if(!init()){
					return ERROR_CODE;
				}
				if(StringUtils.isNull(data)){
					return "";
				}
				if(mLocalKey == null || StringUtils.isNull(data)){
					return null;
				}
				byte[] rawData = data.getBytes("UTF-8");
				byte[] dataBuf = mDataEnCipher.xyEncrypt(rawData, rawData.length, mLocalKey, mLocalKey.length);
				return dataEncode(dataBuf);
			}catch(Throwable e){
	            /*SDK-536 hewengao/20160713 start*/
	            LogManager.e(Constant.TAG, "DataEnCipherApi dataEncrypt",e);
	            /*SDK-536 hewengao/20160713 end*/
			}
			return null;
		}
		/**
		 * 加密数据
		 * @param rawData 待加密的字节数据
		 * @return
		 */
		private static String dataEncode(byte[] rawData){
			if (rawData==null) {
				return "";
			}
			return mDataEnCipher.xyBase64Encode2(rawData, rawData.length);
		}
		
		/*SDK-521 zhaojiangwei 20160707 start */
		private static void nativeLoadLibrary() {
			try {
				System.loadLibrary("xy-algorithm");
				mIsLoad = true;
			}
			catch (Exception e) {
				// TODO: handle exception
			    /*SDK-550 hewengao 20160719 start*/
				LogManager.w(Constant.TAG, "Exception in nativeLoad:", e);
				/*SDK-550 hewengao 20160719 end*/
			}
		}
		/*SDK-521 zhaojiangwei 20160707 end */
	
		private static void loadLibrary() {
			if (mIsLoad) {
				return;
			}
			try {
				File file = new File(Constant.getPARSE_PATH(), "libxy-algorithm.so");
				System.load(file.getCanonicalPath());
				mIsLoad = true;
			}
			catch (Throwable e) {
				mIsLoad = false;
				/* SDK-520 zhaojiangwei 20160705 start */
				try {
					File file = new File(Constant.getPARSE_PATH(), "libxy-algorithm-64.so");
					System.load(file.getCanonicalPath());
					mIsLoad = true;
				}
				catch (Throwable e2) {
                    /* SDK-550 hewengao 20160719 start */
                    LogManager.w(Constant.TAG, "Exception in loadLibrary:", e2);
                    /* SDK-550 hewengao 20160719 end */
					/*SDK-521 zhaojiangwei 20160707 start */
					nativeLoadLibrary();
					/*SDK-521 zhaojiangwei 20160707 end */
				}
				/* SDK-520 zhaojiangwei 20160705 end */
			}
		}
	    /***
	     * 获取单例对象
	     * @return
	     */
	    public synchronized static DataEnCipher getDataEnCipher(){
	    	if(mDataEnCipher == null){
	    		mDataEnCipher = new DataEnCipher();
	    	}
	    	return mDataEnCipher;
	    }
	    /***
	     * 初始化
	     * @return
	     */
	    public synchronized static boolean init(){
	    	try{
		    	if(mLocalKey != null){
		    		return true;
		    	}
		    	getDataEnCipher();
		    	loadLibrary();
		    	if(mLocalKey == null){
		    		mDataEnCipher.getKeyData(1);
		    	}
		    	mDataEnCipher.getChannelData(1);
		    	mDataEnCipher.getChannelData(2);
		    	if(mLocalKey != null){
		    		return true;
		    	}
	    	}catch(Throwable e){
	    		LogManager.e("xiaoyuan", "DataEnCipherApi init ", e);
	    	}
	    	return false;
	    }
	    
}
/* SDK-493 hewngao 20160618 end */
